﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using SuperMap.Desktop;
using SuperMap.UI;
using SuperMap.Mapping;
using SuperMap.Data;
using System.Drawing;

namespace _3._5._5
{
    class MyCtrlAction : CtrlAction
    {
        //鹰眼地图控件
        private MapControl k_mapControl = null;
        //矩形框
        private GeoRectangle k_geoRectangleMapBounds = null;
        //对话框
        private Form k_formLocation = null;

        public MyCtrlAction(IBaseItem caller, IForm formClass)
            : base(caller, formClass)
        {

        }

        override public void Run()
        {
            //To do your work.
            //Example:

            IBaseItem baseItem = this.Caller;
            if (baseItem != null)
            {
                String itemType = baseItem.GetType().ToString().Replace("SuperMap.Desktop.", "");
                MessageBox.Show(String.Format("这是一个 \"{0}\"!", itemType));
            }

            try 
            {
                //设置鹰眼窗体参数
                k_formLocation = new Form();
                k_formLocation.Text = "鹰眼地图";
                k_formLocation.StartPosition = FormStartPosition.CenterScreen;//当前窗口居中
                k_formLocation.FormBorderStyle = FormBorderStyle.SizableToolWindow;//可以调整边框大小
                k_formLocation.TopMost = true;//显示为最顶层窗体

                //设置地图控件方法和属性
                k_mapControl = new MapControl();
                k_mapControl.Dock = DockStyle.Fill;
                k_formLocation.Controls.Add(k_mapControl);

                //弹出窗口
                k_formLocation.Show();

                //获取当前图层
                IFormMap mapWindow = FormClass as IFormMap;

                //在鹰眼视图中显示全图
                k_mapControl.Map.Workspace = mapWindow.MapControl.Map.Workspace;//获取当前工作空间
                Boolean bOpen = k_mapControl.Map.Open(mapWindow.MapControl.Map.Name);//地图是否打开成功
                k_mapControl.Map.ViewEntire();//全幅显示地图

                //如果地图打开成功，添加委托事件
                if (bOpen)
                {
                    SuperMap.Desktop.Application.ActiveApplication.Output.Output("增加鹰眼成功！");
                    mapWindow.MapControl.Map.Drawn += new MapDrawnEventHandler(MapControl_DrawHandler);//添加委托事件
                    Form mapForm = mapWindow as Form;
                    mapForm.FormClosing += new FormClosingEventHandler(MyCtrlAction_FormClosing);
                    k_mapControl.MouseDown += new MouseEventHandler(k_mapControl_MouseDownHandler);
                    k_formLocation.FormClosing += new FormClosingEventHandler(k_formLocation_FormClosing);
                }
                else
                {
                    SuperMap.Desktop.Application.ActiveApplication.Output.Output("增加鹰眼图层失败");
                }
            }

            catch (Exception ex)
            {
                SuperMap.Desktop.Application.ActiveApplication.Output.Output(ex.Message);
            }
            }

        private void MapControl_DrawHandler(object sender,MapDrawnEventArgs e)//获取主窗体的地图范围
        {
            if(k_mapControl==null)
            {
                return;
            }
            //获取当前活动主窗体的图层
            IFormMap mapWindow=FormClass as IFormMap;

            //设置矩形框的属性
            k_geoRectangleMapBounds=new GeoRectangle();
            k_geoRectangleMapBounds.Center=mapWindow.MapControl.Map.Center;
            k_geoRectangleMapBounds.Width=mapWindow.MapControl.Map.ViewBounds.Width;
            k_geoRectangleMapBounds.Height=mapWindow.MapControl.Map.ViewBounds.Height;

            //设置矩形框的风格
            GeoStyle kstyle=new GeoStyle();
            kstyle.LineColor=Color.Red;
            kstyle.LineWidth=0.5;
            kstyle.LineSymbolID=6;
            kstyle.FillBackOpaque=false;
            kstyle.FillOpaqueRate=0;
            k_geoRectangleMapBounds.Style=kstyle;

            //将矩形框添加到跟踪图层
            if(!k_mapControl.IsDisposed)
            {
                //清空跟踪层
                TrackingLayer ktrackLayyer=k_mapControl.Map.TrackingLayer;
                ktrackLayyer.Clear();

                //添加对象
                ktrackLayyer.Add(k_geoRectangleMapBounds as Geometry,"鹰眼图");
                k_mapControl.Map.RefreshTrackingLayer();
                k_mapControl.Refresh();
            }
        }

        private void k_mapControl_MouseDownHandler(object sender,MouseEventArgs e)
        {
            //清空跟踪层
            TrackingLayer ktrackLayyer=k_mapControl.Map.TrackingLayer;
            ktrackLayyer.Clear();

            //改变矩形框位置
            Point pntCenter=new Point(e.X,e.Y);
            k_geoRectangleMapBounds.Center=k_mapControl.Map.PixelToMap(pntCenter);

            //添加对象
            ktrackLayyer.Add(k_geoRectangleMapBounds as Geometry,"鹰眼图");

            //改变主窗体地图可视范围
            IFormMap mapWindow=FormClass as IFormMap;
            mapWindow.MapControl.Map.ViewBounds=k_geoRectangleMapBounds.Bounds;
            mapWindow.MapControl.Map.Refresh();
            mapWindow.MapControl.Refresh();
        }

        //对话框关闭，只要有窗体关闭，都先将鹰眼窗体中的地图关闭
        void FormRelease()
        {
            if(k_mapControl!=null)
            {
                k_mapControl.Map.Close();
                k_mapControl.Dispose();
                k_mapControl=null;
            }
            if(k_formLocation!=null)
            {
                k_formLocation.Dispose();
                k_formLocation=null;
            }
        }

        void k_formLocation_FormClosing(object sender,FormClosingEventArgs e)
        {
            FormRelease();
        }

        void MyCtrlAction_FormClosing(object sender,FormClosingEventArgs e)
        {
            FormRelease();
        }
        
    }
}
